﻿using CommunityToolkit.Mvvm.ComponentModel;
using MG.SmartHMI.Common;
using MG.SmartHMI.Domain;
using MG.SmartHMI.Entity.DAO;
using MG.SmartHMI.Model.Common;
using MG.Util.Log;

namespace MG.SmartHMI.Model
{
    public partial class DispBoxViewModel : BaseItemsControl
    {
        public DispBoxViewModel(RegisterConfig data) : base(data)
        {
        }

        public DispBoxViewModel() : base()
        {
        }

        #region DispDdataBox

        [ObservableProperty]
        private string dispValue;

        public override void UpdateRead()
        {
            DispValue = ModbusCtrl.Instance.UnitRead(RegisterData.DataType, RegisterData.Address).Content;
            EventBus.Default.Log(new LogInfo($"读取 [{RegisterData.Address}]  值 [{DispValue}]", LogLevel.Trace));
        }

        public override void ParsebyBatchData(ref byte[] data, ref bool[] bdata)
        {
            if (RegisterData.DataType.Equals(EnumDataType.BOOL))
            {
                DispValue = ModbusCtrl.Instance.ParseBatchBoolData(ref bdata, int.Parse(RegisterData.Address)).ToString();
            }
            else
            {
                DispValue = ModbusCtrl.Instance.ParseBatchData(ref data, RegisterData.DataType, int.Parse(RegisterData.Address));
            }
        }

        #endregion DispDdataBox
    }
}